<template>
	<view class="login-container">
		<view class="login-header">
			<image class="logo" src="/static/logo.png'" mode="aspectFit"></image>
			<view class="app-name">低空经济</view>
		</view>
		<div class="login-form">
			<u-form :model="formData" ref="uForm">
				<u-form-item prop="mobile">
					<u-input placeholder="请输入手机号码" v-model="formData.mobile" />
				</u-form-item>
				<u-form-item prop="captcha">
					<u-input placeholder="请输入验证码" v-model="formData.captcha" />
					<div class="yanzhengma" v-if="!showSecond" @click="getCode">获取验证码</div>
					<div class="count-down" v-else>{{second}}<span>s</span></div>
				</u-form-item>
			</u-form>
			<div class="login-btn" @click="loginMobile">登录</div>
			<view class="privacy">
				<u-checkbox-group>
					<u-checkbox v-model="checked" @change="onCheck" shape="circle">已阅读并同意低空经济软件的</u-checkbox>
				</u-checkbox-group>
				<text class="privacy-link" @click="openPrivacy">
					《个人信息保护指引》
				</text>
			</view>
		</div>
	</view>
</template>

<script>
import { record } from '@/api'
import { Toast } from '../../utils/utils.js';  
export default {
	data() {
		return {
			checked: false,
			formData: {
				mobile: '',
				captcha: ''
			},
			second: 60,
			showSecond: false,
		}
	},
	methods: {
		onCheck(val) {
			// console.log(val, 'eeessss');
			this.checked = val;
		},
		async getCode () {
			if (this.formData.mobile) {
				const res = await record.sendCode({
					mobile: this.formData.mobile,
					event: 'mobilelogin'
				}).then(res => {
					if (res.response.data.code == 1) {
						this.second = 60;
						this.showSecond = true;
						this.countDown();
					}
				})
			} else {
				Toast('请输入手机号码');
			}
		},
		// 倒计时
		countDown () {
			const setInnt = setInterval(() => {
				if (parseInt(this.second) > 1) {
					this.second = this.second - 1;
				} else {
					clearInterval(setInnt);
					this.second = 60;
					this.showSecond = false;
				}
			}, 1000);
		},
		async loginMobile () {
			if (this.checked) {
				const res = await record.loginByMobile({
					mobile: this.formData.mobile,
					captcha: this.formData.captcha
				}).then(res => {
					console.log(res.response.data.code,'reeessssss');
					if (res.response.data.code == 1) {
						uni.setStorageSync('LOGIN_STATUS_TOKEN', res.response.data.data.userinfo.token);
						uni.setStorageSync('USER_INFO', JSON.stringify(res.response.data.data.userinfo));
						uni.switchTab({
							url: '/pages/mine/mine'
						})
					}
				})
			} else {
				Toast('请同意协议');
			}
		},
		openPrivacy() {
			uni.navigateTo({
				url: '/pages/mine/privacy',
			});
		},
	}
}
</script>

<style scoped>
.login-container {
	min-height: 100vh;
	background: linear-gradient(180deg, #88BEF6 0%, #D1E1FE 100%);
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}
.login-form{
	background: #fff;
	padding: 30px;
	border-radius: 8px;
	position: relative;
	width: 78%;
}
.yanzhengma{
	position: absolute;
	right: 38px;
	top: 102px;
	background-image:-webkit-linear-gradient(bottom,#3881F8, #5EA3FE); 
	-webkit-background-clip:text; 
	-webkit-text-fill-color:transparent;
	/* padding: 5px 10px; */
	z-index: 999;
	line-height: 30px;
	font-weight: bold;
}
.count-down{
	font-size: 16px;
	font-family: PingFang SC-Regular, PingFang SC, sans-serif;
	font-weight: 400;
	color: #FF6700;
	margin-left:21px;
	position: absolute;
	right: 40px;
	top: 107px;
}
.login-header {
	margin-top: 120rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
}
.logo {
	width: 120rpx;
	height: 120rpx;
	margin-bottom: 30rpx;
}
.app-name {
	color: #fff;
	font-size: 40rpx;
	font-weight: bold;
	margin-bottom: 100rpx;
}
.login-btn {
	width: 100%;
	height: 90rpx;
	background: #5c7cfa;
	color: #fff;
	font-size: 32rpx;
	border-radius: 45rpx;
	margin: 40rpx 0;
	box-shadow: 0 8rpx 24rpx rgba(92,124,250,0.2);
	text-align: center;
	line-height: 90rpx;
}
.login-btn.disabled {
	background: #b0b8d1;
	color: #eee;
}
.privacy {
	display: flex;
	align-items: center;
	color: #b0b8d1;
	font-size: 24rpx;
}
.privacy-link {
	color: #5c7cfa;
	margin-left: 8rpx;
	text-decoration: underline;
}
</style>